package com.itchen.service;

import com.itchen.mapper.UserDao;
import com.itchen.pojo.User;
import com.itchen.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 * @author げっしょく
 * @date 2021/9/2
 */
public class UserService {

    private SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    public boolean selectUserName(User user){

        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user1 = mapper.selectByUsername(user.getUsername());

        if (user1 == null) {
            mapper.add(user);
            return true;
        } else {
            return false;
        }
    }

    public User login(String username, String password){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user = mapper.select(username, password);

        sqlSession.close();
        return user;
    }


}
